home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_brickanti.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  2.2 KB  |  82 lines

  1. /* I took wave's lead and renamed brickant to DPBrickAnti.sl -- tal@SpamSucks_cs.caltech.edu */
  2.  
  3. /* 
  4.  * brickant.sl
  5.  *
  6.  * AUTHOR: Darwyn Peachy
  7.  *
  8.  * REFERENCES:
  9.  *    _Texturing and Modeling: A Procedural Approach_, by David S. Ebert, ed.,
  10.  *    F. Kenton Musgrave, Darwyn Peachey, Ken Perlin, and Steven Worley.
  11.  *    Academic Press, 1994.  ISBN 0-12-228760-6.
  12.  */
  13.  
  14.  
  15. #include "k3d_proctext.h"
  16.  
  17. #define BRICKWIDTH      0.25
  18. #define BRICKHEIGHT     0.08
  19. #define MORTARTHICKNESS 0.02
  20.  
  21. #define BMWIDTH         (BRICKWIDTH+MORTARTHICKNESS)
  22. #define BMHEIGHT        (BRICKHEIGHT+MORTARTHICKNESS)
  23. #define MWF             (MORTARTHICKNESS*0.5/BMWIDTH)
  24. #define MHF             (MORTARTHICKNESS*0.5/BMHEIGHT)
  25.  
  26. surface
  27. k3d_brickanti(
  28.     uniform float Ka = 1;
  29.     uniform float Kd = 1;
  30.     uniform color Cbrick = color (0.5, 0.15, 0.14);
  31.     uniform color Cmortar = color (0.5, 0.5, 0.5);
  32.      )
  33. {
  34.     color Ct;
  35.     point Nf;
  36.     float ss, tt, sbrick, tbrick, w, h;
  37.     float scoord = s;
  38.     float tcoord = t;
  39.     float swidth, twidth;
  40.  
  41.     Nf = normalize(faceforward(N, I));
  42.  
  43.     ss = scoord / BMWIDTH;
  44.     tt = tcoord / BMHEIGHT;
  45.  
  46.     if (mod(tt*0.5,1) > 0.5)
  47.         ss += 0.5;  /* shift alternate rows */
  48.  
  49.     swidth = abs(Du(ss)*du) + abs(Dv(ss)*dv);
  50.     twidth = abs(Du(tt)*du) + abs(Dv(tt)*dv);
  51.     tbrick = floor(tt); /* which brick? */
  52.     sbrick = floor(ss); /* which brick? */
  53.  
  54. #if 0
  55.     /* This is the simple antialiasing with "boxstep" */
  56.     ss -= sbrick;
  57.     tt -= tbrick;
  58.  
  59.     w = boxstep(MWF-swidth,MWF,ss)
  60.       - boxstep(1-MWF-swidth,1-MWF,ss);
  61.     h = boxstep(MHF-twidth,MHF,tt)
  62.       - boxstep(1-MHF-twidth,1-MHF,tt);
  63.  
  64. #else
  65.     /* This is the preferred antialiasing using integrals. */
  66. #define frac(x)        mod((x),1)
  67. #define sintegral(ss)  (floor(ss)*(1-2*MWF) + \
  68.                         max(0,frac(ss)-MWF))
  69. #define tintegral(tt)  (floor(tt)*(1-2*MHF) + \
  70.                         max(0,frac(tt)-MHF))
  71.  
  72.     w = (sintegral(ss+swidth) - sintegral(ss))/swidth;
  73.     h = (tintegral(tt+twidth) - tintegral(tt))/twidth;
  74. #endif
  75.  
  76.     Ct = mix(Cmortar, Cbrick, w*h);
  77.  
  78.     /* diffuse reflection model */
  79.     Oi = Os;
  80.     Ci = Os * Ct * (Ka * ambient() + Kd * diffuse(Nf));
  81. }
  82.